/**
 * Ajax同步调用
 * @method syncRequest
 * @param {Object} params json参数对象
 * @param {Boolean} isPost 是否为post方法
 */
function syncRequest(params, isPost) {
    return ajaxRequest(params, null, null, false, isPost)
}

/**
 * Ajax异步调用
 * @method asyncRequest
 * @param {Object} params json参数对象
 * @param {Function} successCallback 成功回调函数
 * @param {Function} errorCallback 失败回调函数
 * @param {Boolean} isPost 是否为post方法
 */
function asyncRequest(params, successCallback, errorCallback, isPost) {
    ajaxRequest(params, successCallback, errorCallback, true, isPost)
}

/**
 * Ajax异步调用
 * @method ajaxRequest
 * @param {Object} params json参数对象
 * @param {Function} successCallback 成功回调函数
 * @param {Function} errorCallback 失败回调函数
 * @param {Boolean} async 是否为异步方法
 * @param {Boolean} isPost 是否为post方法
 */
function ajaxRequest(params, successCallback, errorCallback, async, isPost) {
    var result = { flag: 0, data: {} }
    var url = isPost ?
        '/goform/goform_set_cmd_process' :
        params.cmd ?
        '/goform/goform_get_cmd_process' :
        '/goform/goform_set_cmd_process'
        // url = isPost?"http://localhost:8091/Home/TestPost":'http://localhost:8091/Home/TestGet'
    $.ajax({
        type: !!isPost ? 'POST' : 'GET',
        url: url,
        data: params,
        dataType: 'json',
        async: !!async,
        cache: false,
        error: function(data) {
            if (async) {
                errorCallback(data)
            } else if (data.status == 200) {
                result.flag = 0
                result.data = jQuery.parseJSON('(' + data.responseText + ')')
            }
        },
        success: function(data) {
            if (async) {
                successCallback(data)
            } else {
                result.flag = 1
                result.data = data
            }
        }
    })
    if (!async) {
        return result
    }
}

/**
 * 获取电话本初始化信息
 * @method getPhoneBookReady
 */
function getPhoneBookReady(callback) {
    $.ajax({
        type: 'GET',
        url: "/goform/goform_get_cmd_process",
        data: { cmd: "pbm_init_flag", multi_data: 0 },
        cache: false,
        dataType: "json",
        success: function(data) {
            if (data.pbm_init_flag == 6) {
                callback(false, "error_info");
            } else {
                callback(true, data);
            }

        },
        error: function() {
            callback(false, "error_info");
        }
    });
}

/**
 * 读取电话本容量信息
 * @method getPhoneBookCapacity
 */
function getPhoneBookCapacity(flag, callback) {
    $.ajax({
        type: 'GET',
        url: "/goform/goform_get_cmd_process",
        data: { cmd: "pbm_capacity_info", pbm_location: flag == 1 ? 'pbm_native' : 'pbm_sim' },
        cache: false,
        dataType: "json",
        success: function(data) {
            callback(true, data);
        },
        error: function() {
            callback(false, "error_info");
        }
    });
}

/**
 * 读取电话本数据
 * @method getPhoneBooks
 */
function getPhoneBooks(param, callback) {
    $.ajax({
        type: 'GET',
        url: "/goform/goform_get_cmd_process",
        data: param,
        cache: false,
        dataType: "json",
        success: function(data) {
            callback(true, data.pbm_data || []);
        },
        error: function() {
            callback(false, "error_info");
        }
    });
}

/**
 * 删除联系人
 * @method deletePhoneBooks
 */
function deletePhoneBooks(param, callback) {
    $.ajax({
        type: 'POST',
        url: "/goform/goform_set_cmd_process",
        data: param,
        cache: false,
        dataType: "json",
        success: function(data) {
            if (data.result == "success") {
                checkDeletePhoneBooks();
            } else {
                callback(false);
            }
        },
        error: function() {
            callback(false);
        }
    });

    function checkDeletePhoneBooks() {
        $.ajax({
            url: "/goform/goform_get_cmd_process",
            data: { cmd: "pbm_write_flag" },
            cache: false,
            async: true,
            dataType: "json",
            success: function(result) {
                if (result.pbm_write_flag == "3" || result.pbm_write_flag == "4" || result.pbm_write_flag == "5" || result.pbm_write_flag == "20") {
                    setTimeout(checkDeletePhoneBooks, 1000);
                } else if (result.pbm_write_flag == "0") {
                    callback(true);
                } else {
                    callback(false);
                }
            },
            error: function() {
                callback(false);
            }
        });
    }
}

/**
 * 保存联系人
 * @method savePhoneBook
 */
function savePhoneBook(param, callback) {
    $.ajax({
        type: 'POST',
        url: "/goform/goform_set_cmd_process",
        data: param,
        cache: false,
        dataType: "json",
        success: function(data) {
            if (data.result == "success") {
                checkSavePhoneBook();
            } else {
                callback(false);
            }
        },
        error: function() {
            callback(false);
        }
    });

    function checkSavePhoneBook() {
        $.ajax({
            url: "/goform/goform_get_cmd_process",
            data: { cmd: "pbm_write_flag" },
            cache: false,
            async: true,
            dataType: "json",
            success: function(result) {
                if (result.pbm_write_flag == "1" || result.pbm_write_flag == "2" || result.pbm_write_flag == "20") {
                    setTimeout(checkSavePhoneBook, 1000);
                } else if (result.pbm_write_flag == "0") {
                    callback(true);
                } else {
                    callback(false);
                }
            },
            error: function() {
                callback(false);
            }
        });
    }
}

/**
 * 获取短消息容量信息
 * @method getSMSMessages
 */
function getSmsCapability() {
    var result = {}
    $.ajax({
        type: 'GET',
        url: "/goform/goform_get_cmd_process",
        data: { cmd: "sms_capacity_info" },
        cache: false,
        dataType: "json",
        async: false,
        success: function(data) {
            result.flag = true
            result.data = data
        },
        error: function() {
            result.flag = false
            result.data = "error_info"
        }
    });
    return result
}

/**
 * 获取短消息数据
 * @method getSMSMessages
 */
function getSMSMessages(param, callback) {
    $.ajax({
        type: 'GET',
        url: "/goform/goform_get_cmd_process",
        data: param,
        cache: false,
        dataType: "json",
        success: function(data) {
            callback(true, data.messages || []);
        },
        error: function() {
            callback(false, "error_info");
        }
    });
}

/**
 * 发送短消息
 * @method sendSMS
 */
function sendSMS(param, callback) {
    window.isCanPollingSMS = false
    $.ajax({
        type: 'POST',
        url: "/goform/goform_set_cmd_process",
        data: param,
        cache: false,
        dataType: "json",
        success: function(data) {
            if (data.result == "success") {
                getSmsStatusInfo();
            } else {
                callback(false, 'send_fail_try_again');
                window.isCanPollingSMS = true
            }
        },
        error: function() {
            callback(false, 'send_fail_try_again');
            window.isCanPollingSMS = true
        }
    });

    function getSmsStatusInfo() {
        $.ajax({
            url: "/goform/goform_get_cmd_process",
            data: { cmd: "sms_cmd_status_info", sms_cmd: 4 },
            cache: false,
            async: true,
            dataType: "json",
            success: function(result) {
                if (result.sms_cmd_status_result == "1") {
                    setTimeout(getSmsStatusInfo, 1000);
                } else if (result.sms_cmd_status_result == "3") {
                    callback(true, 'success_info'); //Send failed.
                    window.isCanPollingSMS = true
                } else if (result.sms_cmd_status_result == "2") {
                    callback(false, 'send_fail_try_again');
                    window.isCanPollingSMS = true
                } else {
                    callback(true, 'success_info');
                    window.isCanPollingSMS = true
                }
            },
            error: function() {
                callback(false, 'send_fail_try_again');
                window.isCanPollingSMS = true
            }
        });
    }
}

/**
 * 删除选中短消息
 * @method sendSMS
 */
function deleteMessage(param, callback) {
    window.isCanPollingSMS = false
    $.ajax({
        type: 'POST',
        url: "/goform/goform_set_cmd_process",
        data: param,
        cache: false,
        dataType: "json",
        success: function(data) {
            if (data.result == "success") {
                getSmsStatusInfo();
            } else {
                callback(false, 'delete_fail_try_again');
                window.isCanPollingSMS = true
            }
        },
        error: function() {
            callback(false, 'delete_fail_try_again');
            window.isCanPollingSMS = true
        }
    });

    function getSmsStatusInfo() {
        $.ajax({
            url: "/goform/goform_get_cmd_process",
            data: { cmd: "sms_cmd_status_info", sms_cmd: 6 },
            cache: false,
            async: true,
            dataType: "json",
            success: function(result) {
                if (result.sms_cmd_status_result == "1") {
                    setTimeout(getSmsStatusInfo, 1000);
                } else if (result.sms_cmd_status_result == "3") {
                    callback(true, 'success_info'); //Delete failed, please try again.
                    window.isCanPollingSMS = true
                } else if (result.sms_cmd_status_result == "2") {
                    callback(false, 'delete_fail_try_again');
                    window.isCanPollingSMS = true
                } else {
                    callback(true, 'success_info');
                    window.isCanPollingSMS = true
                }
            },
            error: function() {
                callback(false, 'delete_fail_try_again');
                window.isCanPollingSMS = true
            }
        });
    }
}

/**
 * 查看短消息
 * @method sendSMS
 */
function setSmsRead(param, callback) {
    $.ajax({
        type: 'POST',
        url: "/goform/goform_set_cmd_process",
        data: param,
        cache: false,
        dataType: "json",
        success: function(data) {
            if (data.result == "success") {
                callback(true);
            } else {
                callback(false);
            }
        }
    });

    // function getSmsStatusInfo() {
    //     $.ajax({
    //         url: "/goform/goform_get_cmd_process",
    //         data: { cmd: "sms_cmd_status_info",sms_cmd:7 },
    //         cache: false,
    //         async: true,
    //         dataType: "json",
    //         success: function(result) {
    //             if (result.sms_cmd_status_result == "1") {
    //                 setTimeout(getSmsStatusInfo, 1000);
    //             } else if (result.sms_cmd_status_result == "3") {
    //                 callback(true);
    //             } else {
    //                 callback(false);
    //             }
    //         },
    //         error: function() {
    //             callback(false);
    //         }
    //     });
    // }
}

/**
 * 保存草稿
 * @method saveSMS
 */
function saveSMS(param, callback) {
    window.isCanPollingSMS = false
    $.ajax({
        type: 'POST',
        url: "/goform/goform_set_cmd_process",
        data: param,
        cache: false,
        dataType: "json",
        success: function(data) {
            if (data.result == "success") {
                getSmsStatusInfo();
            } else {
                callback(false, 'sms_save_draft_failed');
                window.isCanPollingSMS = true
            }
        },
        error: function() {
            callback(false, 'sms_save_draft_failed');
            window.isCanPollingSMS = true
        }
    });

    function getSmsStatusInfo() {
        $.ajax({
            url: "/goform/goform_get_cmd_process",
            data: { cmd: "sms_cmd_status_info", sms_cmd: 5 },
            cache: false,
            async: true,
            dataType: "json",
            success: function(result) {
                if (result.sms_cmd_status_result == "1") {
                    setTimeout(getSmsStatusInfo, 1000);
                } else if (result.sms_cmd_status_result == "3") {
                    callback(true, 'success_info');
                    window.isCanPollingSMS = true
                } else if (result.sms_cmd_status_result == "2") {
                    callback(false, 'sms_save_draft_failed');
                    window.isCanPollingSMS = true
                } else {
                    callback(true, 'success_info');
                    window.isCanPollingSMS = true
                }
            },
            error: function() {
                callback(false, 'send_fail_try_again');
                window.isCanPollingSMS = true
            }
        });
    }
}

/**
 * 读取流量统计信息
 * @method saveSMS
 */
function getTrafficSatistics(callback) {
    // var result = getRandomInt(1)
    // if(result==1){
    //     callback(true, {"curr_volume":"0;2300,3991401;2330,3012224;","total_volume":"0;2300,3991401;2330,7003625;"});
    // }else{
    //     callback(true, {"curr_volume":"0;2300,304486;","total_volume":"0;2300,304486;"});
    // }
    $.ajax({
        url: "/goform/goform_get_cmd_process",
        data: { cmd: "total_volume,curr_volume", multi_data: 1 },
        cache: false,
        async: true,
        dataType: "json",
        success: function(result) {
            callback(true, result);
        },
        error: function() {
            callback(false, 'error_info');
        }
    });
}